package hong.jun.task;

import java.util.Random;

/**
 * 说明:战斗任务线程
 * Created on 2023/10/19 15:12
 *
 * @author yanghongjun <1737619387@qq.com>
 */
public class ThreadFightTask extends ThreadTask {

    private final String name1;
    private final String name2;
    Runnable task1;

    public ThreadFightTask(String name1, String name2) {
        this.name1 = name1;
        this.name2 = name2;
    }

    /**
     * @Description:
     * @Param: []
     * @return: void
     * @Author: yanghongjun
     * @Date: 2023/10/19
     */
    @Override
    protected void action() {
        try {
            if (task1 != null) {
                task1.run();
            }
            String threadName = Thread.currentThread().getName();
            System.out.printf("线程%s任务开始执行%s%n", threadName, System.currentTimeMillis());
            long time1 = System.currentTimeMillis();
            //Random random = new Random();
            //Thread.sleep(random.nextInt(10) * 1000L);
            Thread.sleep(2 * 1000L);
            long time2 = System.currentTimeMillis();
            //System.out.println(String.format("线程名:%s 执行%s和%s战斗耗时%s", threadName, name1, name2, time2 - time1));
            System.out.printf("线程%s任务结束执行%s%n", threadName, System.currentTimeMillis());
        } catch (InterruptedException e) {
            System.out.println("线程执行错误");
        }
    }

    /**
     * @Description: 设置其他可选任务
     * @Param: [task1]
     * @return: void
     * @Author: yanghongjun
     * @Date: 2023/10/25
     */
    public void setTask1(Runnable task1) {
        this.task1 = task1;
    }
}
